//
//  AddressListViewController.m
//  MobileShoppingMall
//
//  Created by 冯传昌 on 2021/4/27.
//  Copyright © 2021 冯传昌. All rights reserved.
//

#import "AddressListViewController.h"

#import "CreateNewAddressViewController.h"

@interface AddressListViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong) UITableView *tableView;

@property (nonatomic,strong) UIButton *addAddressBtn;//新建地址按钮

@property (nonatomic,strong) NSMutableArray *dataSourceArr;//数据源数组

@end

@implementation AddressListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = MainBackColor;
    self.topTitle = @"地址";
    self.dataSourceArr = [NSMutableArray array];
    [self initUI];
    [self getData];
}
#pragma mark - data
-(void)getData{
    self.dataSourceArr = [NSMutableArray array];
    [NetWorkingAddress getAddressListWithSuccessBlock:^(id  _Nonnull responseObj) {
        id datas = responseObj[@"data"];
        if ([datas isKindOfClass:[NSArray class]]) {
            NSArray *data = responseObj[@"data"];
            for (NSDictionary *dic in data) {
                AddressModel *model = [AddressModel paraWithDict:dic];
                [self.dataSourceArr addObject:model];
            }
        }
        [self.tableView reloadData];
    }];
}
#pragma mark - UI
-(void)initUI{
    [self.view addSubview:self.tableView];
    [self.view addSubview:self.addAddressBtn];
}
#pragma mark - lazy
- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, kHeight_NavBar, ScreenWidth, ScreenHeight-kHeight_NavBar-kSafe_Bottom-80) style:UITableViewStyleGrouped];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.estimatedRowHeight = 0;
        _tableView.backgroundColor = MainBackColor;
        [_tableView setTableHeaderView:[self tableHeaderFooterView]];
    }
    return _tableView;
}
-(UIView *)tableHeaderFooterView{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 0.1f)];
    return view;
}
- (UIButton *)addAddressBtn{
    if (!_addAddressBtn) {
        _addAddressBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _addAddressBtn.frame = CGRectMake(ScreenWidth/2-100, ScreenHeight-kSafe_Bottom - 70, 200, 40);
        _addAddressBtn.backgroundColor = RGB(71, 108, 255);
        _addAddressBtn.clipsToBounds = YES;
        _addAddressBtn.layer.cornerRadius = 4;
        [_addAddressBtn setTitle:@"新建" forState:normal];
        [_addAddressBtn setTitleColor:UIColor.whiteColor forState:normal];
        [_addAddressBtn addTarget:self action:@selector(createNewAddressClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _addAddressBtn;
}
#pragma mark - UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataSourceArr.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 75;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    AddressListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"AddressListCell"];
    if (!cell) {
        cell = [[AddressListCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"AddressListCell"];
    }
    cell.model = self.dataSourceArr[indexPath.row];
    WEAKSELF(self);
    [cell setEditAddressInfoBlock:^(AddressModel * _Nonnull addressM) {
        [weakSelf updateAddress:addressM];
    }];
    [cell setDeleteAddressBlock:^(AddressModel * _Nonnull addressM) {
        [weakSelf deleteAddress:addressM];
    }];
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    AddressModel *model = self.dataSourceArr[indexPath.row];
    if (self.selectAddressBlock) {
        self.selectAddressBlock(model);
    }
    [self.navigationController popViewControllerAnimated:YES];
}
#pragma mark - SEL
//新建地址
-(void)createNewAddressClick{
    CreateNewAddressViewController *vc = [[CreateNewAddressViewController alloc] init];
    vc.editStatus = NO;
    [self.navigationController pushViewController:vc animated:YES];
    WEAKSELF(self);
    [vc setRefreshAddressListBlock:^{
        [weakSelf getData];
    }];
}
//更新地址信息
-(void)updateAddress:(AddressModel *)model{
    CreateNewAddressViewController *vc = [[CreateNewAddressViewController alloc] init];
    vc.editStatus = YES;
    vc.editModel = model;
    [self.navigationController pushViewController:vc animated:YES];
    WEAKSELF(self);
    [vc setRefreshAddressListBlock:^{
        [weakSelf getData];
    }];
}
//删除地址
-(void)deleteAddress:(AddressModel *)model{
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"确定要删除这个地址吗？" preferredStyle:UIAlertControllerStyleActionSheet];
    UIAlertAction *confirmAction = [UIAlertAction actionWithTitle:@"删除" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
        [self deleteWithModel:model];
    }];
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:nil];
    [alert addAction:confirmAction];
    [alert addAction:cancelAction];
    [self presentViewController:alert animated:YES completion:nil];
}
-(void)deleteWithModel:(AddressModel *)model{
    [NetWorkingAddress deleteAddress:model WithSuccessBlock:^(id  _Nonnull responseObj) {
        [[XNProgressHUD shared] showWithTitle:@"删除成功"];
        [self getData];
    }];
}

@end
